package com.autoMobile.servlet;

import com.autoMobile.bean.ddmUser;
import com.autoMobile.dao.ddmUserDao;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@WebServlet("/ddm/editUser")
public class editUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
    public editUser() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        // 从数据库获取当前用户信息并传递给 JSP 页面
        ddmUserDao dao = new ddmUserDao();
        ddmUser user = dao.findUserByID(id);
        if (user != null) {
            request.setAttribute("user", user);
            request.getRequestDispatcher("/employee/editUser.jsp").forward(request, response);
        } else {
            response.getWriter().write("User not found.");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("UTF-8");
        // 获取表单参数
        int id = Integer.parseInt(request.getParameter("id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String stime = request.getParameter("stime");
        String otime = request.getParameter("otime");
        String shiyong = request.getParameter("shiyong");
        String message = request.getParameter("message");

        // 创建 User 对象
        ddmUser user = new ddmUser();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setStime(stime);
        user.setOtime(otime);
        user.setShiyong(shiyong);
        user.setMessage(message);
System.out.println("更新测试"+user.toString());
        // 更新用户信息
        ddmUserDao dao = new ddmUserDao();
        int success = dao.updateUser(user);

        if (success!=0) {
            response.sendRedirect("shenqing");  // 更新成功后，重定向到用户列表页面
        } else {
            response.getWriter().write("Failed to update user.");
        }
    }
    }
	


